Passed
Pull Request — master (#20)
by
unknown
02:42
created

chiptune.js ➔ pausePauseButton   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
window["libopenmpt"] = {};
2
libopenmpt.locateFile = function (filename) {
0 ignored issues
show
Bug introduced by
The variable libopenmpt seems to be never declared. If this is a global, consider adding a /** global: libopenmpt */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
3
    return "//cdn.jsdelivr.net/gh/deskjet/chiptune2.js@master/" + filename;
4
};
5
libopenmpt.onRuntimeInitialized = function () {
6
    var player;
7
8
    function init() {
9
        if (player == undefined) {
0 ignored issues
show
Best Practice introduced by
Comparing player to undefined using the == operator is not safe. Consider using === instead.
Loading history...
10
            player = new ChiptuneJsPlayer(new ChiptuneJsConfig(-1));
0 ignored issues
show
Bug introduced by
The variable ChiptuneJsPlayer seems to be never declared. If this is a global, consider adding a /** global: ChiptuneJsPlayer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable ChiptuneJsConfig seems to be never declared. If this is a global, consider adding a /** global: ChiptuneJsConfig */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
11
        }
12
        else {
13
            player.stop();
14
            playPauseButton();
15
        }
16
    }
17
18
    function setMetadata(filename) {
19
        var metadata = player.metadata();
20
        if (metadata["title"] != "") {
21
            document.getElementById("title").innerHTML = metadata["title"];
22
        }
23
        else {
24
            document.getElementById("title").innerHTML = filename;
25
        }
26
27
        if (metadata["artist"] != "") {
28
            document.getElementById("artist").innerHTML = "<br />" + metadata["artist"];
29
        }
30
        else {
31
            document.getElementById("artist").innerHTML = "";
32
        }
33
    }
34
35
    function afterLoad(path, buffer) {
36
        document.querySelectorAll("#pitch,#tempo").forEach(e => e.value = 1);
37
        player.play(buffer);
38
        setMetadata(path);
39
        pausePauseButton();
40
    }
41
42
    function loadURL(path) {
43
        init();
44
        player.load(path, afterLoad.bind(this, path));
0 ignored issues
show
Unused Code introduced by
The call to bind does not seem necessary since the function afterLoad declared on line 35 does not use this.
Loading history...
45
    }
46
47
    function pauseButton() {
48
        player.togglePause();
49
        switchPauseButton();
50
    }
51
52
    function switchPauseButton() {
53
        var button = document.getElementById("pause")
54
        if (button) {
55
            button.id = "play_tmp";
56
        }
57
        button = document.getElementById("play")
58
        if (button) {
59
            button.id = "pause";
60
        }
61
        button = document.getElementById("play_tmp")
62
        if (button) {
63
            button.id = "play";
64
        }
65
    }
66
67
    function playPauseButton() {
68
        var button = document.getElementById("pause")
69
        if (button) {
70
            button.id = "play";
71
        }
72
    }
73
74
    function pausePauseButton() {
75
        var button = document.getElementById("play")
76
        if (button) {
77
            button.id = "pause";
78
        }
79
    }
80
81
    var fileaccess = document.querySelector("*");
82
    fileaccess.ondrop = function (e) {
83
        e.preventDefault();
84
        var file = e.dataTransfer.files[0];
85
        init();
86
87
        player.load(file, afterLoad.bind(this, path));
0 ignored issues
show
Unused Code introduced by
The call to bind does not seem necessary since the function afterLoad declared on line 35 does not use this.
Loading history...
Bug introduced by
The variable path seems to be never declared. If this is a global, consider adding a /** global: path */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
88
    }
89
90
    fileaccess.ondragenter = function (e) { e.preventDefault(); }
91
    fileaccess.ondragover = function (e) { e.preventDefault(); }
92
93
    document.querySelectorAll(".song").forEach(function (e) {
94
        e.addEventListener("click", function (evt) {
95
            modurl = evt.target.getAttribute("data-modurl");
0 ignored issues
show
Bug introduced by
The variable modurl seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.modurl.
Loading history...
96
            loadURL(modurl);
97
        }, false);
98
    });
99
100
    document.querySelector("input[name=files]").addEventListener("change", function (evt) {
101
        loadURL(evt.target.files[0]);
102
    });
103
104
    document.querySelector("input[name=submiturl]").addEventListener("click", function () {
105
        var exturl = document.querySelector("input[name=exturl]");
106
        modurl = exturl.value;
0 ignored issues
show
Bug introduced by
The variable modurl seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.modurl.
Loading history...
107
        loadURL(modurl);
108
        exturl.value = null;
109
    });
110
111
    document.querySelector("#play").addEventListener("click", pauseButton, false);
112
113
    document.querySelector("#pitch").addEventListener("input", function (e) {
114
        player.module_ctl_set("play.pitch_factor", e.target.value.toString());
115
    }, false);
116
117
    document.querySelector("#tempo").addEventListener("input", function (e) {
118
        player.module_ctl_set("play.tempo_factor", e.target.value.toString());
119
    }, false);
120
};
121